package com.ruizhong.centercontroll.controller;


import com.ruizhong.centercontroll.aspect.myAnnotation.checkLogin;
import com.ruizhong.centercontroll.entity.TSystem;
import com.ruizhong.centercontroll.service.TOtherSystemService;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import com.ruizhong.centercontroll.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/otherSystem/api")
@CrossOrigin
@Slf4j
public class OtherSystemController {

    @Autowired
    private TOtherSystemService otherSystemService;

    @PostConstruct
    public void checkCenterControlIp(){
        TSystem tSystem = otherSystemService.getById(1);
        if (StringUtils.isEmpty(tSystem.getSystemAddress())){
            String localIp = getLocalIp();
            tSystem.setSystemAddress("http://"+localIp+":8094/#/pageManage");
            otherSystemService.updateById(tSystem);
        }
    }

    public static String getLocalIp() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
                        System.out.println("本机IP地址: " + inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("submitNewSystem")
    @checkLogin("submitNewSystem")
    public Result submitNewSystem(@RequestBody TSystem tSystem) {
        otherSystemService.save(tSystem);
        return new Result(ResultCode.success, "提交成功", null);
    }

    @GetMapping("/getAllSystems")
    @checkLogin("getAllSystems")
    public Result getAllSystems(){
        List<TSystem> systemList = otherSystemService.getAllSystems();
        return new Result(ResultCode.success,null,systemList);
    }

    @GetMapping("/getSystemById/{id}")
    @checkLogin("getSystemById")
    public Result getSystemById(@PathVariable(value = "id") Integer id){
        TSystem tSystem = otherSystemService.getById(id);
        return  new Result(ResultCode.success,null,tSystem);
    }

    @PostMapping("/updateSystemById")
    @checkLogin("updateSystemById")
    public Result updateSystemById(@RequestBody TSystem tSystem){
        boolean update = otherSystemService.lambdaUpdate()
                .eq(TSystem::getId, tSystem.getId())
                .set(TSystem::getSystemName, tSystem.getSystemName())
                .set(TSystem::getSystemAddress, tSystem.getSystemAddress())
                .set(TSystem::getSystemPhoto, tSystem.getSystemPhoto())
                .update();
        if (!update){
            throw new RuntimeException("修改失败");
        }
        return new Result(ResultCode.success,"修改成功",null);
    }

    @DeleteMapping("/deleteSystemById/{id}")
    @checkLogin("deleteSystemById")
    public Result deleteSystemById(@PathVariable(value = "id") Integer id){
        otherSystemService.removeById(id);
        return new Result(ResultCode.success,"删除成功",null);
    }

    @PostMapping("/searchSystemsByName")
    @checkLogin("searchSystemsByName")
    public Result searchSystemsByName(@Nullable@RequestBody String inputForSearch){
       if (StringUtils.isEmpty(inputForSearch)){
           return new Result(ResultCode.success,null,otherSystemService.list());
       }
        List<TSystem> systems = otherSystemService.searchSystemsByName(inputForSearch);
        return new Result(ResultCode.success,null,systems);
    }
}
